<?php
// $Id: table_hotel_album.php 368 2012-05-17 03:14:05Z tomqin@gmail.com $

class Table_Album_Module {
    private $user = null;

    public function __construct($user){
        $this->user = $user;
    }

    public function check_user_right($shop_id){
        if($this->user->status == USER_ADMIN){
            return true;
        }elseif($this->user->status == USER_BUSINESS){
            $uid = $this->user->uid;
            $shop_id = intval($shop_id);
            return Db::getFirst("SELECT count(shop_id) FROM `biz_shop` WHERE admin_uid = '$uid' AND shop_id = '$shop_id'");
        }else{
            return false;
        }
    }

    public function get_data($shop_id, $album_id){
        $shop_id  = intval($shop_id);
        $album_id = intval($album_id);
        return Db::getRow("SELECT * FROM `biz_album` WHERE album_id = '{$album_id}' AND shop_id = '{$shop_id}'");
    }

    public function get_list($shop_id, $album_cat){
        $shop_id   = intval($shop_id);
        $album_cat = intval($album_cat);

        $sql = "SELECT * FROM `biz_album` WHERE shop_id = '{$shop_id}' ". ($album_cat == -1 ? "" : " AND album_cat = '{$album_cat}'") ." ORDER BY ordnum DESC";
        return Db::getAll($sql);
    }

    public function get_list_cat($shop_id){
        $shop_id = intval($shop_id);
        return Db::getKeyArray("SELECT * FROM `biz_album_cat` WHERE `shop_id` = '{$shop_id}' ORDER BY ordnum DESC", 'album_cat');
    }

    public function search(&$search, &$order, &$page, &$total, $eachpage = 10){
        return null;
    }

    public function insert(array $post){
        $data = $this->get_fields($post);
        return Db::insert('biz_album', $data);
    }

    public function import($shop_id, $album_cat, array $post){
        $shop_id   = intval($shop_id);
        $album_cat = intval($album_cat);
        $create_dt = date('Y-m-d H:i:s');
        $stmt = Db::prepare('INSERT INTO `biz_album` (shop_id, album_cat, size, name, picture, description, create_dt, ordnum) VALUES (?,?,?,?,?,?,?,0)');
        $stmt->bind_param('iiissss', $shop_id, $album_cat, $size, $name, $picture, $description, $create_dt);
        foreach($post['pictures'] as $key => $picture){
            $name = $description = $post['names'][$key];
            $size = filesize(UPLOAD_DIR.$picture);
            $stmt->execute();
        }
        $this->count($shop_id, $album_cat);
        return true;
    }

    public function update($shop_id, $album_id, array $post){
        $shop_id  = intval($shop_id);
        $album_id = intval($album_id);
        $data = array(
            'name'        => array_get_string($post, 'name'),
            'description' => array_get_string($post, 'description'),
        );
        return Db::update('biz_album', $data, array('album_id' => $album_id, 'shop_id' => $shop_id));
    }

    public function delete($shop_id, $album_id){
        $data = $this->get_data($shop_id, $album_id);
        if($data){
            $path = dirname(UPLOAD_DIR.$data['picture']);
            $file = pathinfo($data['picture'], PATHINFO_FILENAME);
            $files = glob($path.'/'.$file.'*.*');
            foreach($files as $file){
                echo $file;
                unlink($file);
            }
            Db::query("DELETE FROM `biz_album` WHERE album_id = '$album_id' AND shop_id = '$shop_id'");
            $this->count($shop_id, $data['album_cat']);
        }
    }

    public function count($shop_id, $album_cat){
        $shop_id   = intval($shop_id);
        $album_cat = intval($album_cat);

        return Db::query("UPDATE `biz_album_cat` SET `pic_num` = (SELECT count(album_id) FROM `biz_album` WHERE album_cat = '$album_cat' AND shop_id = '$shop_id')
                           WHERE album_cat = '$album_cat' AND shop_id = '$shop_id'");
    }

    public function sort($shop_id, array $array){
        $stmt = Db::prepare('UPDATE `biz_album` SET ordnum = ? WHERE album_id = ? AND shop_id = ?');
        $stmt->bind_param('iii', $ordnum, $album_id, $shop_id);
        foreach($array as $album_id => $ordnum){
            $stmt->execute();
        }
    }

    protected function get_fields($post){
        return array(
            'shop_id'    => array_get_int($post, 'shop_id'),
            'album_cat'  => array_get_int($post, 'shop_id'),
            'name'       => array_get_string($post, 'name'),
            'picture'    => array_get_string($post, 'picture'),
            'size'       => array_get_int($post, 'size'),
            'description'=> array_get_string($post, 'description'),
            'ordnum'     => array_get_int($post, 'ordnum'),
            'create_dt'  => date('Y-m-d H:i:s'),
        );
    }
}
?>